Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
How to resolve "Entities and POJOs must have a usable public constructor"

I have seen this question several times on SO. however the solution doesn't seem to apply to my problem.

I have a Kotlin data-class that is used as an Entity in Room

@Entity(tableName = "training_session")
data class SessionEntity(
    @PrimaryKey(autoGenerate = false) val id: Long,
    @ColumnInfo(name = "current_state_marker") val currentState: Short,
    @Embedded val states: List<Int>
)

It is producing

> Task :training-infrastructure:kaptDebugKotlin FAILED
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - java.util.List
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - java.util.List

In the same project I have a very similar entity which also has a list and that doesn't produce any errors.


Tried out the answer provided by MikeT, for me it required a small change in the way the converters were defined

data class SessionStateList (val stateList : List<Int>) 

class SessionStateListConverter {

    @TypeConverter
    fun fromArraySessionStateList(sh: List<Int>?): String? {
        return Gson().toJson(sh)
    }
    @TypeConverter
    fun toArraySessionStateList(sh: String?): List<Int>? {
        val listType: Type = object : TypeToken<ArrayList<Int?>?>() {}.type
        return Gson().fromJson(sh,listType)
    }
}

A quick follow-up. I had mentioned that I have another Entity that has an Embedded val something: List<Int> and I had not noticed any compiler errors.

The reason, I had not noticed any compiler errors was because the entity was not included in the @Database annotation.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You cannot have a List/Array etc as a column type. So your issue is centred on @Embedded val states: List<Int>

You could have a POJO e.g. StatesHolder :-

data class StatesHolder(
    val stateList: List<Int>
)

and then have

@Entity(tableName = "training_session")
data class SessionEntity(
    @PrimaryKey(autoGenerate = false) val id: Long,
    @ColumnInfo(name = "current_state_marker") val currentState: Short,
    val states: StatesHolder
)
  • Note that you cannot Embed StatesHolder as then that just inserts List. If you want to Embed then you have to Embed a wrapper that uses a StatesHolder.

You will then need TypeConverters to convert to and from a StatesHolder object to a type that can be stored. Probably a String and Probably a JSON respresentation of the StatesHold object e.g.

class Converters {

    @TypeConverter
    fun fromStatesHolder(sh: StatesHolder): String {
        return Gson().toJson(sh)
    }
    @TypeConverter
    fun toStatesHolder(sh: String): StatesHolder {
        return Gson().fromJson(sh,StatesHolder::class.java)
    }
}

You additionally need to use @TypeConverters annotation that defines the Converts::class. If coded at the @Database level the converters have full scope.

So after @Database(.....) you could have :-

@TypeConverters(Converters::class)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda